package com.example.usercenter.sys.service;

import com.alibaba.fastjson.JSONObject;
import com.example.usercenter.common.base.Page;
import com.example.usercenter.sys.dao.AuthorityDao;
import com.example.usercenter.sys.dao.UserGroupDao;
import com.example.usercenter.sys.entity.Authority;
import com.example.usercenter.sys.entity.OrgUser;
import com.example.usercenter.sys.entity.UserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.example.usercenter.common.base.BaseService;
import com.example.usercenter.common.base.BaseDao;
import com.example.usercenter.sys.dao.UserDao;

import com.example.usercenter.sys.entity.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService extends BaseService<User, Long> implements UserDetailsService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private UserDao userDao;
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private UserGroupDao userGroupDao;
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private AuthorityDao authorityDao;

    protected BaseDao<User, Long> getDao(){
            return userDao;
    }

    @Override
    public boolean insert(User entity) {
        if(userDao.insert(entity)>0){
            if(entity.getGroup()!=null){
                UserGroup group = userGroupDao.get(entity.getGroup().getId());
                if(!group.isAutoAuth()
                        && entity.getRoles()!=null && entity.getRoles().size()>0){
                    userDao.insertUserRole(entity);
                }
            }
            if(entity.getOrgUsers()!=null && entity.getOrgUsers().size()>0){
                userDao.insertOrgUser(entity);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean update(User entity) {
        if(userDao.update(entity)>0){
            userDao.deleteOrgUser(entity.getId());
            userDao.deleteUserRole(entity.getId());
            if(entity.getGroup()!=null){
                UserGroup group = userGroupDao.get(entity.getGroup().getId());
                if(!group.isAutoAuth()
                        && entity.getRoles()!=null && entity.getRoles().size()>0){
                    userDao.insertUserRole(entity);
                }
            }
            if(entity.getOrgUsers()!=null && entity.getOrgUsers().size()>0){
                userDao.insertOrgUser(entity);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean sensitiveUpdate(User entity) {
        return super.sensitiveUpdate(entity);
    }

    @Override
    public boolean delete(Long id) {
        if(userDao.delete(id)>0){
            userDao.deleteOrgUser(id);
            userDao.deleteUserRole(id);
            return true;
        }
        return false;
    }

    @Override
    public User loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userDao.getByUsername(s);
        if(user.getGroup().isAutoAuth()){
            //自动授权，根据用户组查询关联权限
            JSONObject params = new JSONObject();
            params.put("groupId", user.getGroup().getId());
            List<Authority> auths = authorityDao.query(params);
            user.setAuths(auths);
        }
        return user;
    }

    public User getByUsername(String username){
        return userDao.getByUsername(username);
    }

    public List<OrgUser> queryByUsername(String username) {
        return userDao.queryByUsername(username);
    }

    @Override
    public Page findByPage(Map<String, Object> params) {
        return new Page<User>(getDao().queryByPage(params), getDao().count(params));
    }
}